﻿Type=Activity
Version=2.71
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim btnOK As Button
	Dim edtPassword As EditText
	Dim edtUser As EditText
	Dim lblVer As Label
	
	Dim FSql As String
	Dim FUserCode,FUserPass,FUserName As String
	Dim FDBPass As Boolean
	Dim FOldSession As String
	
	Dim HttpJobTool As HttpJob
	
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormLogin")
	Activity.Title = "登陆"
	lblVer.Text = "版本:" & PublicVar.GV_Version 
	
	InitLayOut
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub InitLayOut
	Activity.AddMenuItem("选项","MenuItemLoginConfig")

End Sub

Sub MenuItemLoginConfig_Click
	If Sender = "选项" Then
		StartActivity(LoginConfig)

	End If
	
End Sub	

Sub btnOK_Click
	Dim sUserID As String
	Dim sPass As String
	sUserID = edtUser.Text.Trim
	sPass = edtPassword.Text.Trim
	
	If sUserID ="" Then	
		ToastMessageShow("请输入工号!", False)	
		edtUser.RequestFocus
		Return
	End If
	
	FUserCode = sUserID
	FUserPass = sPass
	
	'登录远程服务器 
	UserLogin
	
	'暂时不用登陆则
	'AfterLogin
	
	PublicVar.GV_UserCode = FUserCode
	
End Sub

Sub UserLogin As Boolean
	Dim sURL,sUser,sPass,sText As String
	
	FOldSession = PublicVar.GV_UserSession
	
	PublicVar.GV_UserSession = ""
	
	sUser = PublicVar.GV_DesTool.Base64encrypt(FUserCode)
	If FDBPass = False Then
		FUserPass = PublicVar.GV_DesTool.Base64encrypt(FUserPass)
	End If	
	sPass = FUserPass
	
	sText = "User=" & sUser & "&PassWord=" & sPass
	 
	sURL = PublicVar.GetServerUrl&"/login"

	HttpJobTool.Initialize("Job1",Me)
	HttpJobTool.PostString(sURL,sText)

	ProgressDialogShow("正在登录，请稍候...")	

	Return True
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      If Job.JobName = "Job1" Then
				LoginSuccess(Job)
      End If
   Else
		LoginError(Job)
   End If
   Job.Release
End Sub

Sub AfterLogin

	StartActivity("SelectCLLB")  '
End Sub

Sub LoginSuccess(Job As HttpJob)
	Dim sResult,sSession,sUserName,sCode,sData,sVersion As String
	Dim fVersion As Double
	Dim mapResult As Map
	
	ProgressDialogHide
	sResult = Job.GetString.Trim
	
	Log(sResult)
	'获取Session
	PublicVar.GV_JSonTool.Initialize(sResult)
	mapResult = PublicVar.GV_JSonTool.NextObject
	
	sCode = mapResult.Get("Code")
	If sCode<>"0" Then
		sResult = mapResult.Get("Message")
		ToastMessageShow("登录失败"&CRLF &sResult, True)	
		Return
	End If
	
	sData = mapResult.Get("Data")
	sData = PublicVar.GV_DesTool.Base64decrypt(sData)
	PublicVar.GV_JSonTool.Initialize(sData)
	mapResult = PublicVar.GV_JSonTool.NextObject
	
	sSession = mapResult.Get("Session")
	
	If sSession="" Then
		ToastMessageShow("获取Session失败", True)
		PublicVar.GV_UserSession = FOldSession
		Return
	End If
	PublicVar.GV_UserSession = sSession
	
	sVersion = mapResult.Get("Ver")
	If sVersion = "" Then
		ToastMessageShow("获取版本号失败", True)
		Return		
	End If
	fVersion = sVersion
	If fVersion>PublicVar.GV_Version Then
		ToastMessageShow("客户端版本号太低", True)
		Return			
	End If
	
	sUserName = mapResult.Get("UserName")
	FUserName = sUserName
	
	PublicVar.GV_Login = True
	
	AfterLogin
	ToastMessageShow (sUserName&",欢迎您！", True)
	
	Log("登录成功")

End Sub

Sub LoginError(Job As HttpJob)
	Dim sMsg As String
	ProgressDialogHide
	PublicVar.GV_UserSession = FOldSession
	
  Log("Error: " & Job.ErrorMessage)
	sMsg = "登录失败."& CRLF & Job.ErrorMessage 
  ToastMessageShow(sMsg, True)

End Sub